package com.espirit.eap.manager;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class NbrRuleSourceManager {
	@Resource
	GenericService genericService;
	
	Map<String,NbrRuleSource> nbrRuleSources = null;
	
	public NbrRuleSource getPrefix(){
		return getNbrRuleSource(NbrRuleSource.PREFIX);
	}
	
	public NbrRuleSource getYear(){
		return getNbrRuleSource(NbrRuleSource.YEAR);
	}

	public NbrRuleSource getMonth(){
		return getNbrRuleSource(NbrRuleSource.MONTH);
	}
	
	public NbrRuleSource getDay(){
		return getNbrRuleSource(NbrRuleSource.DAY);
	}
	
	public NbrRuleSource getUserId(){
		return getNbrRuleSource(NbrRuleSource.USERID);
	}
	
	private NbrRuleSource getNbrRuleSource(String nbrRuleSourceCode){
		if (nbrRuleSources == null){
			nbrRuleSources = new HashMap<String, NbrRuleSource>();
		} 
		if (nbrRuleSources.get(nbrRuleSourceCode) == null){
			NbrRuleSource model = new NbrRuleSource();
			model.setCode(nbrRuleSourceCode);
			model = genericService.searchAll(model).getData(0);
			if (model != null){
				nbrRuleSources.put(nbrRuleSourceCode, model);
			} else {
				nbrRuleSources.put(nbrRuleSourceCode, null);
			}
		}
		return nbrRuleSources.get(nbrRuleSourceCode);
	}
	
}
